package com.wms.business;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.wms.client.BuildHttpClient;
import com.wms.domain.PDParam;
import com.wms.domain.PDParamPath;

/**
 * @author YQR
 *
 */
public class PDBusiness {
	private Logger logger = LoggerFactory.getLogger(PDBusiness.class);
	private final String PATTERN[] = new String[] { "yyyy-MM-dd HH:mm:ss" };
	
    
	/**
	 * 创建盘点单
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void createMangPD() throws ClientProtocolException, IOException {
			
			logger.info("开始进行--------------PC创建盘点单-----------------");
			PDParam param =new PDParam();
			param.setReviewType(PDParamPath.bmType);
			param.setLocationIdList(Arrays.asList((PDParamPath.locationId).split(",")));
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.BuildPDpath, JSON.toJSONString(param));
			logger.info("这里是\"PC盘点单管理新增或发起盘点\"响应：{}", result);
		
	}

	/**
	 * 获取最新reviewId
	 * @return
	 * @throws ParseException
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public String getReviewid() throws ParseException, ClientProtocolException, IOException {
		String reviewId = "";
		
			logger.info("开始进行--------------查询新增状态盘点单-----------------");
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.Reviewidpath, PDParamPath.ReviewidbodyParams);
			logger.info("这里是\"查询新增状态盘点单\"响应：{}", result);
			// 获取rows中结果
			ArrayList<PDParam> params = JSON.parseObject(result.getJSONObject("result").get("rows").toString(),
					new TypeReference<ArrayList<PDParam>>() {
					});

			long min = 0;
			// 当前时间
			long time1 = Calendar.getInstance().getTime().getTime();
			for (int i = 0; i < params.size(); i++) {
				PDParam job = params.get(i);
				String createTime = job.getCreateTime();
				// 获取到的时间
				long time2 = DateUtils.parseDate(createTime, PATTERN).getTime();
				String reviewIdresult = job.getReviewId();
				long diff = time1 - time2;
				if (min == 0 || (min != 0 && diff < min)) {
					min = diff;
					reviewId = reviewIdresult;
				}
			}
			logger.info("这里是获取到的盘点单ID : {}", reviewId);
			return reviewId;
		
		
	}

	
	/**
	 * 获取areaId
	 * @param reviewId
	 * @return areaId
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public String getareaId(String reviewId) throws ClientProtocolException, IOException {
		PDParam param = new PDParam();
		param.setReviewId(reviewId);
//		logger.info("=====================================对象属性值{}", param.toString());
	
			logger.info("开始进行--------------查询盘点单库区信息-----------------");
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.selectPDMsgpath, JSON.toJSONString(param));
			logger.info("这里是\"RF查询盘点单库区信息\"响应：{}", result);
			JSONArray jsonArray = result.getJSONArray("result");
//			 (JSONObject) jsonArray.get(0);
			JSONObject object =	jsonArray.getJSONObject(0);
			String areaId = object.getString("key");
//			String areaId = result.getJSONArray("result").getJSONObject(0).getString("key");
			logger.info("提取出来的库区ID ：{}", areaId);
			return areaId;		
	}

	/**
	 * 领取
	 * @param reviewId
	 * @param areaId
	 * @return 
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public PDParam receivePD(String reviewId, String areaId) throws ClientProtocolException, IOException {
		PDParam param = new PDParam();
		param.setReviewId(reviewId);
		param.setAreaId(areaId);
//		logger.info("=======赋值给对象 reviewId与areaId值\t{}",param.toString());
		
			logger.info("开始进行--------------RF领取计划盘点单-----------------");
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.receivePDPath, JSON.toJSONString(param));
			logger.info("这里是\"RF领取盘点\"响应：{}", result);
			// 开始提取赋值给对象，json字符穿转换为对象 param = JSON.parseObject(result.getJSONObject("result").toString(), new TypeReference<PDParam>() {});	
			param = JSON.parseObject(result.getJSONObject("result").toString(), PDParam.class);
			return param;		
	}

	
	/**
	 * 扫码
	 * @param param
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void Scan_code(PDParam param,String type) throws ClientProtocolException, IOException {
		param.setType(type);
		logger.info("=====================================扫码参数对象属性值{}", param.toString());
		
			logger.info("开始进行--------------扫码-----------------");			
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.Scan_codepath, JSON.toJSONString(param));
			logger.info("这里是\"RF扫描\"响应：{}", result);
		
	}

	/**
	 * 确认1
	 * @param param
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void affirm1(PDParam param,int CheckCountParam) throws ClientProtocolException, IOException {
		String CheckCount=String.valueOf(CheckCountParam);
		param.setCheckCount(CheckCount);
		param.setProductDateExist("true");
		param.setConfirm("false");
		param.setYes("false");
		logger.info("=====================================确认1参数对象属性值{}", param.toString());
		
			logger.info("开始进行--------------确认1-----------------");
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.confirmpath, JSON.toJSONString(param));
			logger.info("这里是\"确认1\"响应：{}", result);
		
	}

	/**
	 * 确认2
	 * @param param
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void affirm2(PDParam param,int CheckCountParam) throws ClientProtocolException, IOException {
		String CheckCount=String.valueOf(CheckCountParam);
		param.setCheckCount(CheckCount);
		param.setProductDateExist("true");
		param.setConfirm("true");
		param.setYes("false");
		logger.info("=====================================确认2参数对象属性值{}", param.toString());
			logger.info("开始进行--------------确认2-----------------");
			JSONObject	result = BuildHttpClient.doPost(PDParamPath.confirmpath, JSON.toJSONString(param));
			logger.info("这里是\"确认2\"响应：{}", result);
		
	}
	
	/**
	 * 
	 * 发起复盘
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 * @param reviewIds
	 * @param threshold 复盘的阀值
	 */
	public void recheck(List<String> reviewIds,int threshold) throws ClientProtocolException, IOException{
		PDParam param=new PDParam();
		param.setReviewIds(reviewIds);
		param.setThreshold(threshold);
		logger.info("开始进行--------------复盘-----------------");
		JSONObject	result = BuildHttpClient.doPost(PDParamPath.recheckpath, JSON.toJSONString(param));
		logger.info("这里是\"复盘\"响应：{}", result);
	}
	
	
	/**
	 * 
	 * 中断盘点
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public void interrupt(List<String> reviewIds) throws ClientProtocolException, IOException{
		PDParam param=new PDParam();
		param.setReviewIds(reviewIds);
		logger.info("开始进行--------------中断盘点-----------------");
		JSONObject	result = BuildHttpClient.doPost(PDParamPath.interruptpath, JSON.toJSONString(param));
		logger.info("这里是\"复盘\"响应：{}", result);
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
